﻿using System;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using MVCThemes.Interfaces;

namespace MVCThemes.WebUI {

	public class MvcApplication : HttpApplication {

		public static void RegisterGlobalFilters(GlobalFilterCollection filters) {
			filters.Add(new HandleErrorAttribute());
		}

		public static void RegisterRoutes(RouteCollection routes) {
			routes.RouteExistingFiles = false;
			routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
			routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" });

			routes.IgnoreRoute("{*allcss}", new { allcss = @".*\.css(/.*)?" });

			routes.IgnoreRoute("themes/{*pathInfo}");
			routes.IgnoreRoute("content/{*pathInfo}");

			routes.MapRoute(
				"Default", // Route name
				"{controller}/{action}/{id}", // URL with parameters
				new { controller = "Home", action = "Index", id = UrlParameter.Optional }
			);
		}

		protected void Application_Start() {
			ViewEngines.Engines.Clear();
			ViewEngines.Engines.Add(new ThemeViewEngine(DependencyResolver.Current.GetService<IThemeProvider>(), DependencyResolver.Current.GetService<IThemeURLProvider>()));

			AreaRegistration.RegisterAllAreas();
			RegisterGlobalFilters(GlobalFilters.Filters);
			RegisterRoutes(RouteTable.Routes);
		}
	}
}